#!/usr/bin/python3
import re

#------------------------
def sbin():
    print ('1.查找出passwd文件里有多少行里包含sbin字符串，统计出行数')
    f = open("passwd")
    a = f.read()
    n = len(re.findall(r'.*sbin.*\n',a))
    print ("total line number is: %s" % n)
    f.close()
sbin()

#---------------------------
def uid():
    print ("2.查找出passwd文件里uid是3位数字的uid")
    f = open("passwd")
    a = f.read()
    n = re.findall(r'\w+:\w+:(?P<uid>\d{3}):.*\n',a)
    print (n)
    f.close()
uid()

#---------------------------
def feng():
    print ("3.查找出用户名里包含feng，uid是4位数字，shell是bash的行")
    f = open("passwd")
    a = f.read()
    n = re.findall(r'(feng):\w+:(?P<uid>\d{4}):.*',a)
    print (n)
    f.close()
feng()

#---------------------------
def sub():
    print ("4.将passwd文件里的sbin替换为cali")
    f = open("passwd")
    a = f.read()
    n = re.sub("sbin","cali",a)
    f2 = open("new_passwd.txt","w+")
    f2.write(n)
    print ("write success")
    f2.close()
    f.close()
sub()

#---------------------------
def ip():
    print ("5.查找出/var/log/secure文件里的ip地址")
    f = open("/var/log/secure")
    a = f.read()
    n = re.findall(r'(feng):\w+:(?P<uid>\d{4}):.*',a)
    print (n)
    f.close()
